package com.shujia.wyh.day16;

/*
        统计大串中小串出现的次数
        举例：在字符串"woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagunwoaijavawozhenaijavawozhendeaijavawozhwoaijavawozhenaijavawozhendeaijavawozhwoaijavawozhenaijavawozhendeaijavawozhwoaijavawozhenaijavawozhendeaijavawozhwoaijavawozhenaijavawozhendeaijavawozhwoaijavawozhenaijavawozhendeaijavawozhwoaijavawozhenaijavawozhendeaijavawozhwoaijavawozhenaijavawozhendeaijavawozhwoaijavawozhenaijavawozhendeaijavawozh"
 */
public class StringTest6 {
    public static void main(String[] args) {
        //定义一个大串
//        String bigString = "woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagunwoaijavawozhenaijavawozhendeaijavawozhwoaijavawozhenaijavawozhendeaijavawozhwoaijavawozhenaijavawozhendeaijavawozhwoaijavawozhenaijavawozhendeaijavawozhwoaijavawozhenaijavawozhendeaijavawozhwoaijavawozhenaijavawozhendeaijavawozhwoaijavawozhenaijavawozhendeaijavawozhwoaijavawozhenaijavawozhendeaijavawozhwoaijavawozhenaijavawozhendeaijavawozhjava";

        String bigString = "dasjavadasdjava";
        //定义一个小串
        String minString = "java";

        //定义一个变量统计小串出现的次数
        int count = 0;

        //找出小串在大串中的第一次出现的位置
        int index = bigString.indexOf(minString); //3

        //有可能第一次在大串中就找不到小串
        if (index == -1) {
            System.out.println("大串中不存在该小串");
        } else {
            //"dasdjava "
            while (index != -1) {
                count++; //1,2
                //截取字符串
                int startIndex = index + minString.length(); //7 8
                bigString = bigString.substring(startIndex); //dasdjava
                index = bigString.indexOf(minString); // 4
            }
        }

        System.out.println("小串" + minString + "在大串中出现的次数为：" + count + "次");

//        String s ="dsadsa";
//        String substring = s.substring(6);
//        System.out.println(substring);

    }
}
